import Vue from 'vue'
import VueRouter from 'vue-router'

import SysUser from "@/views/sys/user/list"
import SysRole from "@/views/sys/role/list"
import SysDict from "@/views/sys/dict/list"
import OperateLog from "@/views/sys/operateLog/list"
import RetryLogTask from "@/views/sys/retryLogTask/list"
import TimerTask from "@/views/sys/timerTask/list"

import ProductCategory from "@/views/product/category/list"
import ProductBaseAttr from "@/views/product/baseAttr/list"
import ProductSaleAttr from "@/views/product/saleAttr/list"
import ProductSpu from "@/views/product/spu/list"
import OrderList from "@/views/order/order/list"

import SysLogin from "@/views/sys/login"
import Home from "@/views/home"
import userUtils from "@/utils/userUtils"

Vue.use(VueRouter)

// 路由配置
const routes = [
  {
    path: "/",
    name: "SysLogin",
    component: SysLogin,
    meta: { requiresAuth: false, desc: "登录" }
  }, {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: { requiresAuth: true, desc: "主页" },
    children: [
      {
        path: "/sys/user",
        name: "SysUser",
        component: SysUser,
        meta: { requiresAuth: true, desc: "系统用户" }
      },
      {
        path: "/sys/role",
        name: "SysRole",
        component: SysRole,
        meta: { requiresAuth: true, desc: "系统角色" }
      },
      {
        path: "/sys/dict",
        name: "SysDict",
        component: SysDict,
        meta: { requiresAuth: true, desc: "系统字典" }
      },
      {
        path: "/sys/operateLog",
        name: "OperateLog",
        component: OperateLog,
        meta: { requiresAuth: true, desc: "操作日志" }
      },
      {
        path: "/sys/retryLogTask",
        name: "RetryLogTask",
        component: RetryLogTask,
        meta: { requiresAuth: true, desc: "重试任务" }
      },
      {
        path: "/sys/timerTask",
        name: "TimerTask",
        component: TimerTask,
        meta: { requiresAuth: true, desc: "定时任务" }
      },
      {
        path: "/product/category",
        name: "ProductCategory",
        component: ProductCategory,
        meta: { requiresAuth: true, desc: "商品分类" }
      },
      {
        path: "/product/baseAttr",
        name: "ProductBaseAttr",
        component: ProductBaseAttr,
        meta: { requiresAuth: true, desc: "属性分组" }
      },
      {
        path: "/product/saleAttr",
        name: "ProductSaleAttr",
        component: ProductSaleAttr,
        meta: { requiresAuth: true, desc: "销售属性" }
      },
      {
        path: "/product/spu",
        name: "ProductSpu",
        component: ProductSpu,
        meta: { requiresAuth: true, desc: "商品列表" }
      },
      {
        path: "/order/order",
        name: "OrderList",
        component: OrderList,
        meta: { requiresAuth: true, desc: "订单列表" }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !userUtils.isLogin()) {
    // 未登录，跳转到登录页面
    Vue.prototype.$message.error('请重新登录');
    next({ path: '/' })
  } else {
    next()
  }
})

export default router
